package com.hue.controller;


import com.alibaba.excel.util.StringUtils;
import com.hue.entity.EduVideo;
import com.hue.entity.R;
import com.hue.service.EduVideoService;
import com.hue.client.VodFeignService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 课程视频 前端控制器
 * </p>
 *
 * @author 姚凤桥
 * @since 2021-06-13
 */
@RestController
@CrossOrigin
@RequestMapping("/video")
public class EduVideoController {
    @Autowired
    private EduVideoService eduVideoService;
    @Autowired
    private VodFeignService vodFeignService;

    @PostMapping("/add")
    public R add(@RequestBody EduVideo eduVideo) {
        eduVideoService.save(eduVideo);
        return R.ok();
    }

    @PostMapping("/update")
    public R update(@RequestBody EduVideo eduVideo) {
        boolean b = eduVideoService.updateById(eduVideo);
        return b ? R.ok() : R.error();
    }

    //删除小节,并删除视频
    @GetMapping("/deleteById/{id}")
    public R deleteById(@PathVariable("id") String id) {
        //查询小节对应的视频id
        EduVideo eduVideo = eduVideoService.getById(id);
        String videoId = eduVideo.getVideoSourceId();
        if(!StringUtils.isEmpty(videoId)){
            //根据视频id删除视频
            vodFeignService.delete(videoId);
        }
        //删除小节
        boolean b = eduVideoService.removeById(id);
        return b ? R.ok() : R.error();
    }

    @GetMapping("/findById/{id}")
    public R findById(@PathVariable("id")String id){
        EduVideo video = eduVideoService.getById(id);
        return R.ok().data("video",video);
    }
}

